package com.uxwine.cmm;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

/**
 * <pre>
 * 这个类主要用来调用Servie的。
 * 本类对Service交互进行了封装
 * 使用方法如下：
 * 
 * public static final String ACTION_SERVICE_START = "com.uxwine.SERVICE_START";
 * CCommand cmd = new Command(ACTION_SERVICE_START);
 * cmd.put("key", 1)
 * 	.put("key2",true)
 * 	.put("key3","uxwine")
 * 	.command(getContext());
 * 
 * </pre>
 * 
 * @author 	zhangzhongming<eddie.whu@gmail.com>
 * @date	2013.4.27
 * */
public class UxCommand
{
	protected Bundle mData;
	protected String msAction;
	
	public UxCommand(String action)
	{
		mData = new Bundle();
		msAction = action;
	}
	
	public UxCommand set(Bundle data)
	{
		mData = data;
		return this;
	}
	
	public UxCommand put(String key, int value)
	{
		mData.putInt(key, value);
		return this;
	}
	
	public UxCommand put(String key, long value)
	{
		mData.putLong(key, value);
		return this;
	}
	
	public UxCommand put(String key, boolean value)
	{
		mData.putBoolean(key, value);
		return this;
	}
	
	public UxCommand put(String key, String value)
	{
		mData.putString(key, value);
		return this;
	}
	
	public UxCommand put(String key, Parcelable value)
	{
		mData.putParcelable(key, value);
		return this;
	}
	
	public UxCommand put(String key, Double value)
	{
		mData.putDouble(key, value);
		return this;
	}
	
	public UxCommand put(String key, Float value)
	{
		mData.putFloat(key, value);
		return this;
	}
	
	public void command(Context ctx)
	{		
		Intent intent = build();
		ctx.startService(intent);
	}
	
	public void command(Context ctx, boolean inner)
	{		
		Intent intent = build();
		if(inner)
			intent.setPackage(ctx.getPackageName());
		ctx.startService(intent);
	}
	
	public void broadcast(Context ctx)
	{		
		Intent intent = build();
		ctx.sendBroadcast(intent);
	}
	
	public void broadcast(Context ctx, boolean inner)
	{		
		Intent intent = build();
		if(inner)
			intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);
	}
	
	public Intent build()
	{
		final Intent intent = new Intent(msAction);
		intent.putExtras(mData);
		return intent;
	}
}
